-- Copyright (c) Huawei Technologies Co., Ltd. 2025. All rights reserved.
--
-- this file licensed under the Mulan PSL v2.
-- You can use this software according to the terms and conditions of the Mulan PSL v2.
-- You may obtain a copy of Mulan PSL v2 at: http://license.coscl.org.cn/MulanPSL2
--
-- THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS OR
-- IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY OR FIT FOR A PARTICULAR
-- PURPOSE.
-- See the Mulan PSL v2 for more details.
--
-- Author: auto generate
-- Create: 2025-9-2
-- Description: DO NOT EDIT; Code generated by "${project_name}_app.lua.mako"

local class = require 'mc.class'
local service = require 'Hello_openUBMC.service'
local logging = require 'mc.logging'
-- 新增ipmi相关文件
local custom_msg = require 'messages.custom'
local ipmi_cmds = require 'Hello_openUBMC.ipmi.ipmi'

local Hello_openUBMC = class(service)

function Hello_openUBMC:ctor()

end

function Hello_openUBMC:init()
    logging:notice("Hello_openUBMC:init start")
    self.super.init(self)
    self.hello_openUBMC = self:CreateHelloOpenUBMC(function(object)
        object.ObjectName = "HelloOpenUBMC_1"
        object.WelcometoOpenUBMC = "Welcome to openUBMC"
        object.Health = 1
    end)
    -- ipmi
    self:register_ipmi_cmd(ipmi_cmds.FirstIpmiCmd, self.first_ipmi_cmd)
    logging:notice("MyEepromModel run start")
    self:register_myeeprommodel_callback()
    logging:notice("Hello_openUBMC: init end")
end

function Hello_openUBMC.first_ipmi_cmd(req, ctx)
    -- 参数校验(按需)
    local data_len = req.DataLen
    local data = req.Data
    if #data ~= data_len then
        logging:error('first ipmi cmd error: invalid req length')
        error(custom_msg.IPMIRequestLengthInvalid())
    end
    -- 业务处理
    -- 构造响应体
    local rsp = ipmi_cmds.FirstIpmiCmd.rsp.new()
    rsp.CompletionCode = 0x00
    rsp.ManufactureId = 0x000000
    logging:operation(ctx, 'Hello_openUBMC', 'first ipmi cmd success, Data:%s.', data)
    return rsp
end

function Hello_openUBMC:register_myeeprommodel_callback()
    object_manage.on_add_object(self.bus, function(class_name, object, position)
        if class_name == 'MyEepromModel' then
            self.my_eeprom_model = object
        end
    end)
    object_manage.on_add_object_complete(self.bus, function(position)
    end)
    object_manage.on_delete_object(self.bus, function(class_name, object, position)
        if self.my_eeprom_model == object then
            self.my_eeprom_model = nil
        end
    end)
    object_manage.on_delete_object_complete(self.bus, function(position)
    end)
end

return Hello_openUBMC
